home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / Snippets / Toolbox / CustomIcon / CustIcon.c next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  7.6 KB  |  339 lines  |  [TEXT/KAHL]

  1. // This snippet shows how to use custom document icons in an application.  
  2. // The correct procedure for doing this is to add the Icon family 
  3. // to the document and set bit 10 of the finder info.
  4. //
  5. // An elegant way of adding the icon family is to use one of the routines 
  6. // described in the Icon Utilities chapter of Inside Macintosh More Toolbox 
  7. // Essentials:   ForEachIconDo().  You can define  an action proc that is called 
  8. // each time for each icon an an icon suite.
  9. //
  10. // AppleLink: NICKT
  11. //
  12. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  13.  
  14.  
  15. #define    WWIDTH        470
  16. #define    WHEIGHT        330
  17.  
  18. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  19. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  20.  
  21. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  22. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  23.  
  24.  
  25. #include <icons.h>
  26. #include <finder.h>
  27.  
  28. //------------------------------------------------------------------------------
  29. // enumerated types for the menus
  30.  
  31. enum {
  32.     mApple = 128,
  33.     mFile
  34. } ;
  35.  
  36. enum {
  37.     iAbout = 1
  38. } ;
  39. enum {
  40.     iNew = 1,
  41.     iIcon,
  42.     iUnused1,
  43.     iQuit = 4
  44. } ;
  45.  
  46. //------------------------------------------------------------------------------
  47.  
  48. static Boolean pQuitFlag = false ;
  49.  
  50. //------------------------------------------------------------------------------
  51. // function prototypes
  52.  
  53. void InitToolbox( void ) ;
  54. void MainEventLoop( void ) ;
  55. void HandleKeyPress(EventRecord *event) ; 
  56. void HandleMenuCommand(long menuResult) ;
  57. void AdjustMenus( void ) ;
  58. pascal OSErr    MyIconAction( ResType    theType,
  59.                               Handle    *theIcon,
  60.                               void        *myDataPtr )  ;
  61. OSErr TouchDir(short vRefNum, long dirID) ;
  62.  
  63. //-----------------------------------------------------------------------------
  64.  
  65.  
  66. void HandleMenuCommand(long menuResult)
  67. {
  68.     short        menuID;
  69.     short        menuItem;
  70.     Str255        daName;
  71.     DialogPtr    theDialog ; 
  72.     short        itemHit ;
  73.     SFTypeList    myTypes = { '????' } ;
  74.     Handle        myIconSuite ;
  75.     FSSpec        theFSSpec ;
  76.     OSErr        theErr ;
  77.     short        theRef ;
  78.     
  79.     short        savedResFile ;
  80.     short        theResFile ;
  81.     
  82.     FInfo        myFndrInfo ;
  83.         
  84.     StandardFileReply    theSFReply ;
  85.  
  86.     menuID = HiWrd(menuResult);
  87.     menuItem = LoWrd(menuResult);
  88.     switch ( menuID ) {
  89.         case mApple:
  90.             switch ( menuItem ) {
  91.                 case iAbout:
  92.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  93.                     SetDialogDefaultItem(theDialog, 1) ;
  94.  
  95.                     do {
  96.                         ModalDialog ( nil, &itemHit );
  97.                     } while( itemHit != ok ) ;
  98.                     DisposDialog ( theDialog );
  99.                     break;
  100.                     
  101.                 default:
  102.                     GetItem(GetMHandle(mApple), menuItem, daName);
  103.                     (void) OpenDeskAcc(daName);
  104.                     break;
  105.             }
  106.             break;
  107.         case mFile:
  108.             switch ( menuItem ) {
  109.                 case iNew:
  110.                     // create a file
  111.                     // put up a standard file put dialog to get the name
  112.                     StandardPutFile("\pCreate File", "\pUntitled", &theSFReply) ;
  113.                     if( theSFReply.sfGood ) {
  114.                         FSpCreateResFile( &theSFReply.sfFile, '????', 'TEXT', smSystemScript) ;
  115.                         theErr = ResError();
  116.                     }
  117.                     break ;
  118.                     
  119.                 case iIcon:
  120.                     // add icon family to file
  121.                     
  122.                     // put up std get file dlog
  123.                     StandardGetFile( nil, -1, myTypes, &theSFReply ) ;
  124.                     if( theSFReply.sfGood ) {
  125.                         
  126.                         // save the current resource file
  127.                         savedResFile = CurResFile();
  128.                         
  129.                         // Load the icon suite we want to set up
  130.                         theErr = GetIconSuite( &myIconSuite, 129, svAllAvailableData )  ;
  131.                         
  132.                         // get the finder info for the file
  133.                         theErr = FSpGetFInfo( &theSFReply.sfFile, &myFndrInfo ) ;
  134.                         
  135.                         // set bit 10 (has custom icon) and unset the inited flag
  136.                         // kHasBeenInited is 0x0100 so the mask will be 0xFEFF:
  137.                         
  138.                         myFndrInfo.fdFlags = 0xFEFF & (myFndrInfo.fdFlags | kHasCustomIcon ) ;
  139.                         
  140.                         // write it back
  141.                         theErr = FSpSetFInfo( &theSFReply.sfFile, &myFndrInfo ) ;
  142.                         
  143.                         // just check it already has a resource fork
  144.                         FSpCreateResFile( &theSFReply.sfFile, '????', 'TEXT', smSystemScript) ;
  145.                         theErr = ResError();
  146.                         
  147.                         // open the resfile the user picked
  148.                         theResFile = FSpOpenResFile ( &theSFReply.sfFile, fsWrPerm);
  149.                         theErr = ResError() ;
  150.                     
  151.                         UseResFile ( theResFile );
  152.                         theErr = ResError() ;
  153.                         
  154.                         theErr = ForEachIconDo( myIconSuite, svAllAvailableData, MyIconAction, nil ) ;
  155.                         CloseResFile( theResFile );
  156.                         
  157.                         theErr = ResError() ;
  158.                         
  159.                         UseResFile ( savedResFile );
  160.                         
  161.                     }
  162.                     
  163.  
  164.                     break ;
  165.                 case iQuit:
  166.                 
  167.                     pQuitFlag = true;
  168.                     break;
  169.             }
  170.             break;
  171.             
  172.     }
  173.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  174. }
  175.  
  176. //---------------------------------------------------------------------------
  177. // write out the icon to the currently set res file
  178.  
  179. pascal OSErr    MyIconAction( ResType    theType,
  180.                               Handle    *theIcon,
  181.                               void        *myDataPtr )
  182. {
  183.  
  184.     OSErr    theErr = noErr ;
  185.     long    lByteCnt = GetHandleSize( *theIcon );
  186.  
  187.     // theIcon is already a resource.  If we try to do an AddResource, it will fail
  188.     // with error -194, to prevent this, call detach resource.  You will need
  189.     // to reload the suite if you want to use it after this.
  190.     
  191.     DetachResource( *theIcon ) ;
  192.     if(( theErr = ResError()) != noErr )
  193.         return theErr ;    
  194.         
  195.     // write out each resource we are passed by ForEachIconDo
  196.     AddResource ( *theIcon, theType, kCustomIconResource, "\pCustom Finder Icon" );
  197.  
  198.     // check we are OK
  199.     theErr = ResError() ;
  200.  
  201.     return theErr ;
  202.     
  203. }
  204.  
  205. //---------------------------------------------------------------------------
  206.  
  207. void MainEventLoop()
  208. {
  209.     EventRecord     event;
  210.     WindowPtr       window;
  211.     short           thePart;
  212.     Rect            screenRect;
  213.     Point            aPoint = {100, 100};
  214.     GrafPtr            oldPort ;
  215.  
  216.     while( !pQuitFlag )
  217.     {
  218.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  219.         {
  220.             AdjustMenus() ;
  221.  
  222.             switch (event.what) {
  223.                 case mouseDown:
  224.                 
  225.                     thePart = FindWindow( event.where, &window );
  226.                     
  227.                     switch( thePart ) {
  228.                         case inMenuBar: 
  229.                             HandleMenuCommand(MenuSelect(event.where));
  230.                             break;
  231.                         
  232.                         case inDrag:
  233.                             break ;
  234.                     
  235.                         case inContent:
  236.                             break ;
  237.                     
  238.                         case inGoAway:
  239.                             break ;
  240.                             
  241.                         default:
  242.                             break ;
  243.                     }
  244.                     break ;
  245.                             
  246.                         
  247.                 case updateEvt:
  248.                     break ;
  249.                     
  250.                 case keyDown:
  251.                 case autoKey:
  252.                     HandleKeyPress(&event);
  253.                     break;
  254.                     
  255.                 case diskEvt:
  256.                     if ( HiWrd(event.message) != noErr ) 
  257.                         (void) DIBadMount(aPoint, event.message);
  258.                     break;
  259.                     
  260.                 case osEvt:
  261.                 case activateEvt:
  262.                     break;
  263.  
  264.  
  265.             }
  266.         }
  267.     }
  268. }
  269.  
  270. //------------------------------------------------------------------------------
  271.  
  272. void HandleKeyPress(EventRecord *event)
  273. {
  274.     char    key;
  275.     long    lByteCnt ;
  276.  
  277.     key = event->message & charCodeMask;
  278.     
  279.     // just check to see if we want to quit...
  280.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  281.         HandleMenuCommand(MenuKey(key));
  282.     } 
  283. }
  284.  
  285. //------------------------------------------------------------------------------
  286.  
  287. void AdjustMenus( void ) 
  288. {
  289.     // ha - we don't got no menus
  290. }
  291.  
  292. //------------------------------------------------------------------------------
  293.  
  294. main()
  295. {
  296.     InitToolbox() ;
  297.     
  298.     MainEventLoop();
  299. }
  300.  
  301. //------------------------------------------------------------------------------
  302.  
  303. void InitToolbox()
  304. {
  305.     OSErr         retCode;
  306.     long         gestResponse;
  307.     Handle        menuBar = nil;
  308.     EventRecord event;
  309.     short        count;
  310.  
  311.  
  312.     InitGraf((Ptr) &qd.thePort);
  313.     InitFonts();
  314.     InitWindows();
  315.     InitMenus();
  316.     TEInit();
  317.     InitDialogs(0L);
  318.     InitCursor();
  319.  
  320.     // initialize application globals
  321.     
  322.     pQuitFlag = false;
  323.     
  324.     
  325.     menuBar = GetNewMBar(128);                // Read menus into menu bar, MBAR res id is 128
  326.     
  327.     if ( menuBar == nil )
  328.          ExitToShell();                        // if we dont have it then quit - your app 
  329.                                              // needs a dialog here
  330.  
  331.     SetMenuBar(menuBar);                    // Install menus
  332.     DisposHandle(menuBar);
  333.     
  334.     AddResMenu(GetMHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  335.  
  336.     DrawMenuBar();
  337. }
  338.  
  339.